/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.transfer.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.transfer.bean.entity.RiskTransferLogEntity;
import com.hopes.bizz.risk.transfer.bean.vo.RiskDataReportStatistics;
import com.hopes.bizz.risk.transfer.bean.vo.RiskTransferLogVo;
import com.hopes.bizz.risk.transfer.service.RiskTransferLogService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 风险数据对接日志
 *
 * @author pig
 * @date 2024-02-01 20:17:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskTransferLog")
@Tag(description = "riskTransferLog", name = "风险数据对接日志管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskTransferLogController {

    private final RiskTransferLogService riskTransferLogService;

    /**
     * 分页查询
     *
     * @param page            分页对象
     * @param riskTransferLog 风险数据对接日志
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getRiskTransferLogPage(Page page, RiskTransferLogEntity riskTransferLog) {
        LambdaQueryWrapper<RiskTransferLogEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(riskTransferLog.getOrgId()), RiskTransferLogEntity::getOrgId, riskTransferLog.getOrgId())
                .eq(Objects.nonNull(riskTransferLog.getStatus()), RiskTransferLogEntity::getStatus, riskTransferLog.getStatus());
        return R.ok(riskTransferLogService.page(page, wrapper).convert(entity -> BeanUtil.copyProperties(entity, RiskTransferLogVo.class)));
    }


    /**
     * 通过id查询风险数据对接日志
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('transfer_riskTransferLog_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(riskTransferLogService.getById(id));
    }

    /**
     * 新增风险数据对接日志
     *
     * @param riskTransferLog 风险数据对接日志
     * @return R
     */
    @Operation(summary = "新增风险数据对接日志", description = "新增风险数据对接日志")
    @SysLog("新增风险数据对接日志")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('transfer_riskTransferLog_add')")
    public R save(@RequestBody RiskTransferLogEntity riskTransferLog) {
        return R.ok(riskTransferLogService.save(riskTransferLog));
    }

    /**
     * 修改风险数据对接日志
     *
     * @param riskTransferLog 风险数据对接日志
     * @return R
     */
    @Operation(summary = "修改风险数据对接日志", description = "修改风险数据对接日志")
    @SysLog("修改风险数据对接日志")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('transfer_riskTransferLog_edit')")
    public R updateById(@RequestBody RiskTransferLogEntity riskTransferLog) {
        return R.ok(riskTransferLogService.updateById(riskTransferLog));
    }

    /**
     * 通过id删除风险数据对接日志
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除风险数据对接日志", description = "通过id删除风险数据对接日志")
    @SysLog("通过id删除风险数据对接日志")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('transfer_riskTransferLog_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskTransferLogService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param riskTransferLog 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('transfer_riskTransferLog_export')")
    public List<RiskTransferLogEntity> export(RiskTransferLogEntity riskTransferLog) {
        return riskTransferLogService.list(Wrappers.query(riskTransferLog));
    }

    /**
     * @Description 统计企业数据对接情况
     * @Author lyb
     * @Date 2024/2/21
     **/
    @GetMapping("/calculateRiskDataReport")
    public R calculateRiskDataReport() {
        RiskDataReportStatistics riskDataReportStatistics = riskTransferLogService.calculateRiskDataReport();
        return R.ok(riskDataReportStatistics);
    }

    /**
     * @Description 根据批次ID查询上传数据
     * @Author lyb
     * @Date 2024/2/21
     **/
    @GetMapping("/getReportData/{batchId}")
    public R getReportData(@PathVariable String batchId) {
        return R.ok(riskTransferLogService.getReportDataByBatchId(batchId));
    }
}